package com.next

import grails.converters.JSON
import grails.gorm.transactions.Transactional
import groovy.transform.CompileStatic
import groovy.transform.TypeChecked

@Transactional
@CompileStatic
@TypeChecked
class CreditReportService
{
    /**
     * 调用个人征信接口
     * @author xiaruikun
     * @ModifiedDate 2017-5-5
     */

    def generateReportAll(CreditReportProvider provider, OpportunityContact opportunityContact)
    {
        def br
        String response = ""
        String params
        def contact = opportunityContact?.contact
        if (contact?.cellphone)
        {
            params = "cellphone=" + URLEncoder.encode(contact?.cellphone, "UTF-8") + "&fullName=" + URLEncoder.encode(contact?.fullName, "UTF-8") + "&idNumber=" + URLEncoder.encode(contact?.idNumber, "UTF-8")
        } else
        {
            params = "fullName=" + URLEncoder.encode(contact?.fullName, "UTF-8") + "&idNumber=" + URLEncoder.encode(contact?.idNumber, "UTF-8")
        }
        String apiUrl = provider?.apiUrl + "?" + params
        URL url = new URL(apiUrl)
        println "url:" + url
        try
        {
            HttpURLConnection connection = (HttpURLConnection) url.openConnection()
            connection.setDoOutput(true)
            connection.setRequestMethod("GET")
            br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))
            def line
            while ((line = br.readLine()) != null)
            {
                response += line
            }
            if (response)
            {
                println "征信查询成功"
                def json = JSON.parse(response)
                return json

            }
        }
        catch (Exception e)
        {
            println "征信查询失败"
           
        }
    }

    /**
     * 调用企业征信接口
     * author xiaruikun*/

    def generatePengyuan(CreditReportProvider provider, Company company)
    {
        def br
        String response = ""
        String params
        params = "company=" + URLEncoder.encode(company?.company, "UTF-8")
        if (company?.companyCode)
        {
            params += "&companyCode=" + URLEncoder.encode(company?.companyCode, "UTF-8")
        } else
        {
            params += "&companyCode="
        }
        if (company?.industry)
        {
            params += "&industry=" + URLEncoder.encode(company?.industry?.name, "UTF-8")
        } else
        {
            params += "&industry="
        }
        String apiUrl = provider?.apiUrl + "?" + params
        URL url = new URL(apiUrl)
        println "url:" + url
        try
        {
            HttpURLConnection connection = (HttpURLConnection) url.openConnection()
            connection.setDoOutput(true)
            connection.setRequestMethod("GET")
            br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))
            def line
            while ((line = br.readLine()) != null)
            {
                response += line
            }
            if (response)
            {
                println "征信查询成功"
                def json = JSON.parse(response)
                return json

            }
        }
        catch (Exception e)
        {
            println "征信查询失败"
           
        }
    }

    /**
     * author xiaruikun*/

    def queryBlackList(CreditReportProvider provider, def params)
    {
        def br
        String response = ""
        String apiUrl = provider?.apiUrl + "?" + params
        URL url = new URL(apiUrl)
        println "url:" + url
        try
        {
            HttpURLConnection connection = (HttpURLConnection) url.openConnection()
            connection.setRequestMethod("GET")
            connection.setRequestProperty("Content-Type", "application/json")
            connection.setRequestProperty("Accept", "*/*")
            connection.setRequestProperty("Connection", "Keep-Alive")
            connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible MSIE 8.0 Windows NT 6.1)")
            connection.connect()
            br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))
            def line
            while ((line = br.readLine()) != null)
            {
                response += line
            }
            if (response)
            {
                println "Default黑名单查询成功"
                def json = JSON.parse(response)
                return json
            }
        }
        catch (Exception e)
        {
            println "获取Default黑名单失败"
           
        }
    }

    def isNumber(String score)
    {
        try
        {
            if (score)
            {
                score.toInteger()
                return true
            } else
            {
                return false
            }
        }
        catch (NumberFormatException e)
        {
            return false
        }
    }

}
